//-
  ztncui - ZeroTier network controller UI
  Copyright (C) 2017-2021  Key Networks (https://key-networks.com)
  Licensed under GPLv3 - see LICENSE for details.

extends network_layout

//- Don't display that title
block title

//- Replace the network title with the editable one
block network_title
  h2
    | 网络 
    a#change-name(href='#')
      span#name= network.name
      i.glyphicon.glyphicon-pencil(style='font-size: 20px;')
    input#name-input.form-control(type='text' style='width: 200px; display: none;')
    |  (#{network.nwid}):
  script.
    $(function() {
      var nwurl = '/controller/network/#{network.nwid}';
      var name = !{JSON.stringify(network.name)};

      function toggleNameEditor(show) {
        $('#change-name').css('display', !show ? '' : 'none');
        $('#name-input').css('display', show ? 'inline-block' : 'none');
      }

      function submit() {
        var newName = $('#name-input').val();
        if (newName != name) {
          name = newName;
          $.post(nwurl + '/name', {'name': name})
            .done(function () {
              $('#name').text(newName);
            });
        }
        toggleNameEditor(false);
      }

      $('#change-name').on('click', function() {
        toggleNameEditor(true);
        $('#name-input').val(name);
        $('#name-input').focus();
      });
      $('#name-input').on('focusout', submit);
      $('#name-input').keypress(function (e) {
        if (e.which == 13) submit();
      });
    });

block net_content
  - const nwurl = '/controller/network/' + network.nwid;

  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/private') role='button')
    = network.private ? "私有" : "公有"
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/easy') role='button') 简易安装
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/routes') role='button') 路由
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/ipAssignmentPools') role='button') 地址分配池
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/v4AssignMode') role='button') IPv4 分配模式
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/v6AssignMode') role='button') IPv6 分配模式
  a.btn.btn-primary(style="margin: 5px" href=(nwurl + '/dns') role='button') DNS

  if (members !== undefined)
    script.
      $(function() {
        const url = "#{nwurl}/members";
        $('.authCheck').on('click', function() {
          $.post(url, {'id': this.value, 'auth': this.checked});
        });
        $('.bridgeCheck').on('click', function() {
          $.post(url, {'id': this.value, 'activeBridge': this.checked});
        });
        $('.text').on('change', function() {
          $.post(url, {'id': this.name, 'name': this.value});
        });
      });
    h3#members 成员 (#{members.length})
    form(method='POST' action='')
      table.table.table-responsive.table-striped.table-hover
        tr
          td(width='3%')
            = ''
          td(width='20%')
            | 成员名称
          td(width='10%')
            | 成员ID
          td(width='10%')
            | 经授权的
          td(width='10%')
            | 主动桥接
          td(width='17%')
            | IP分配
          td(width='17%')
            | 节点状态
          td(width='13%')
            | 节点地址 / 延迟
        each member in members
          - const peer = member.peer;
          tr
            - const url = nwurl + '/member/' + member.id
            td
              a(href=url + '/delete')
                i.glyphicon.glyphicon-trash
            td
              input.form-control.text(type='text' name=member.id value=member.name)
            td
              a(href=url) #{member.id}
            td
              input.authCheck(type='checkbox' value=member.id checked=(member.authorized? true : false))
            td
              input.bridgeCheck(type='checkbox' value=member.id checked=(member.activeBridge? true : false))
            td
              each ipAssignment in member.ipAssignments
                a(href=nwurl + '/member/' + member.id + '/ipAssignments')
                  each digit in ipAssignment
                    = digit
                  = ' '
              else
                a(href=nwurl + '/member/' + member.id + '/ipAssignments')
                  | IP分配
            td
              if (peer && peer.latency != -1 && peer.versionMajor != -1)
                if (peer.latency != -1)
                  span(style='color: green;')
                    | 在线 (v#{peer.version})
                else
                  span(style='color: orange;')
                    | 转发 (v#{peer.version})
              else if (member.id == zt_address)
                span(style='color: green;') 控制器
              else
                span(style='color: red;') 离线
            td
              if (peer)
                each path in peer.paths
                  - const [ip, port] = path.address.split('/');
                  = ip
                  span(style='color: gray;') /#{port}
                  = ' '
                if (peer.latency != -1)
                  br
                  | (#{peer.latency} ms)
        else
          .alert.alert-info
            strong 此网络上没有成员-请用户加入 #{network.nwid}

  a.btn.btn-default(href='' name='refresh' role='button') 刷新

  h3#detail 网络详细信息
  each value, key in network
    .row(style='margin: 5px 0;')
      .col-sm-2
        a(href= network.nwid + '/' + key) #{key}:
      .col-sm-10
        +json_value(value)

  a.btn.btn-default(href='/controller/networks' name='networks' role='button' style='margin-top: 10px;') 网络